import Vue from 'vue'
import VueRouter from 'vue-router'
import home from '../components/home.vue'
import about from '../components/about.vue'
import movie from '../components/movie.vue'
import tab1 from '../components/tab1.vue'
import tab2 from '../components/tab2.vue'
import login from '../components/login.vue'
import main from '../components/main.vue'
Vue.use(VueRouter)

const router = new VueRouter({
  routes:[
    {path:'/',redirect:'/home'},
    {path:'/home',component:home},
    {
      redirect:'/about/tab1',
      path:'/about',
      component:about,
      children:[
        {path:'tab1',component:tab1},
        {path:'tab2',component:tab2}
      ]
    },
    {
      path:'/movie/:id',
      component:movie,
      props:true
    },
    {path:'/login',component:login},
    {path:'/main',component:main},
  ]
})

//为router实例对象声明全局前置守卫
router.beforeEach(function(to,from,next) {
  //to表示将要访问的路由信息对象
  //from表示将要离开的路由信息对象
  console.log(to);
  //放行
  // next()
  if (to.path === '/main') {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    }else {
      next('/login')
    }
  }else {
    next()
  }

})
export default router